#ifndef _COMMON_H_
#define _COMMON_H_
#include <vector>
#include "cJSON.h"
#include "common_func.h"

#define EFFECT_LIST_STR "Effect List"
#define EFFECT_STR "Effect"
#define NAME_STR "name"
#define DURATION_STR "duration"
#define PATH_STR "path"
#define CAPTURE_LEFTTOP_X "capture_left_top_x"
#define CAPTURE_LEFTTOP_Y "capture_left_top_y"
#define CAPTURE_RIGHTTOP_X "capture_right_top_x"
#define CAPTURE_RIGHTTOP_Y "capture_right_top_y"
#define CAPTURE_RIGHTBOTTOM_X "capture_right_bottom_x"
#define CAPTURE_RIGHTBOTTOM_Y "capture_right_bottom_y"
#define CAPTURE_LEFTBOTTOM_X "capture_left_bottom_x"
#define CAPTURE_LEFTBOTTOM_Y "capture_left_bottom_y"
#define CAPTURE_WIDTH "capture_width"
#define CAPTURE_HEIGHT "capture_height"
#define CAPTURE_HFLIP "capture_hflip"
#define CAPTURE_VFLIP "capture_vflip"
#define CAPTURE_CONTRAST "capture_contrast"
#define CAPTURE_SENSITIVE "capture_sensitive"
#define CAPTURE_SETTING TEXT("capture_setting.dat")
#define SOFTWARE_INFO TEXT("Info.dat")
#define SOFT_CAPTION "caption"

#define REG_DEFI        TEXT("defi_interactive_system")
#define REG_DEFI_PATH   TEXT("path")
#define REG_DEFI_AUTO_PLAY TEXT("autoPlay")
#define REG_DEFI_PLAY_MODE TEXT("playMode")

#define EFFECT_NAME_SIZE 128
#define EFFECT_ITEM_PASTE_FMT   TEXT("defi interactive projection format")
#define DEFI_WINDOWS_CLASS_NAME TEXT("defi_interactive_system_class_name")
#define DEFI_HIDE_LOADING_WINDOW WM_USER + 518

typedef struct _effect_item_ {
	TCHAR effect_name[EFFECT_NAME_SIZE];
	int duration;
	BOOL haveSetting;
	TCHAR effect_path[MAX_PATH];
}EFFECT_ITEM, *PEFFECT_ITEM;

typedef std::vector<PEFFECT_ITEM> EFFECT_ITEM_LIST;
typedef EFFECT_ITEM_LIST *PEFFECT_ITEM_LIST;


__inline void free_effect_list(PEFFECT_ITEM_LIST pEff_list)
{
	PEFFECT_ITEM pItem;
	if(pEff_list){
		while(!pEff_list->empty()){
			pItem = pEff_list->back();
			pEff_list->pop_back();
			free(pItem);
		}
	}
}

__inline BOOL StoreSoftPath(TCHAR *path)
{
	HKEY hkey;
	BOOL retValue = FALSE;
	if(RegCreateKey(HKEY_CURRENT_USER, REG_DEFI, &hkey) != ERROR_SUCCESS){
		return FALSE;
	}

	if(RegSetValueEx(hkey, REG_DEFI_PATH, NULL, REG_SZ, (BYTE *)path, _tcslen(path) * sizeof(TCHAR)) == ERROR_SUCCESS){
		retValue = TRUE;
	}
	RegCloseKey(hkey);
	return retValue;
}

__inline BOOL GetSoftPath(TCHAR *out_path, int out_buf_len)
{
	HKEY hkey;
	int retValue = FALSE;
	DWORD dtype = REG_SZ;
	
	if(RegOpenKey(HKEY_CURRENT_USER, REG_DEFI, &hkey) != ERROR_SUCCESS){
		return FALSE;
	}
	if(RegQueryValueEx(hkey, REG_DEFI_PATH, NULL, &dtype, (BYTE*)out_path, (LPDWORD)&out_buf_len) == ERROR_SUCCESS){
		retValue = TRUE;
	}
	RegCloseKey(hkey);
	return retValue;
}

__inline BOOL GetSoftCaption(TCHAR *out_caption, int out_buf_len)
{
	cJSON *jroot = NULL, *jitem = NULL, *jvalue = NULL;
	BOOL bret = FALSE;
	char *buf = NULL;
	int buflen;
	TCHAR filePath[MAX_PATH];

	if(!out_caption)
		return bret;
	_tcscpy(out_caption, TEXT("Defi interactive system"));
	GetSoftPath(filePath, MAX_PATH);
	_tcscat(filePath, SOFTWARE_INFO);
	buf = readFileToBuf(filePath, &buflen);
	if(buf){
		DecodeData(buf, buflen);
		jroot = cJSON_Parse(buf);
		if(!jroot){
			goto getsoftcaption_exit;
		}
		jitem = cJSON_GetObjectItem(jroot, SOFT_CAPTION);
		if(!jitem)
			goto getsoftcaption_exit;
		if(jitem->valuestring){
			memset(out_caption, 0, sizeof(TCHAR) * out_buf_len);
			MultiByteToWideChar(CP_ACP, 0, jitem->valuestring, -1, out_caption, out_buf_len);
			bret = TRUE;
		}
	}

getsoftcaption_exit:
	if(jroot)
		cJSON_Delete(jroot);
	if(buf)
		free(buf);
	return bret;
}

__inline BOOL CreateEffectData(const TCHAR *softPath, PEFFECT_ITEM pItem)
{
	if(softPath == NULL || pItem == NULL){
		return FALSE;
	}
	cJSON *jroot,*effectItem;
	char tmpbuf[100] = {0};
	TCHAR effectFullPath[MAX_PATH] = {0};
	BOOL bret = FALSE;
	jroot = cJSON_CreateObject();
	if(jroot){
		effectItem = cJSON_CreateObject();
		if(!effectItem)
			goto createeffectdata;
		WideCharToMultiByte(CP_ACP, 0, pItem->effect_name, -1, tmpbuf, 100, NULL, NULL);
		cJSON_AddStringToObject(effectItem, NAME_STR, tmpbuf);
		cJSON_AddNumberToObject(effectItem, DURATION_STR, pItem->duration);
		memset(tmpbuf, 0, 100);
		WideCharToMultiByte(CP_ACP, 0, pItem->effect_path, -1, tmpbuf, 100, NULL, NULL);
		cJSON_AddStringToObject(effectItem, PATH_STR, tmpbuf);
		_tcscpy(effectFullPath, softPath);
		_tcscat(effectFullPath, TEXT("effects\\"));
		_tcscat(effectFullPath, pItem->effect_name);
		_tcscat(effectFullPath, TEXT("\\effect.dat"));
		cJSON_AddItemToObject(jroot, EFFECT_STR, effectItem);
		char *effectdataString = cJSON_Print(jroot);
		bret = writeBufToFile(effectFullPath, effectdataString, strlen(effectdataString));
		free(effectdataString);
	}

createeffectdata:
	if(jroot)cJSON_Delete(jroot);
	return bret;
}

typedef enum {PM_ORDER = 1,
	PM_REPEAT_ONE,
	PM_REPEAT_ALL,
	PM_SHUFFLE
} PlayMode;

#endif